using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;
using MainGame.GamePlay;

namespace MainGame.Menu
{

  public interface INewGameMenu
  {
    event MenuEvent OnBackClicked;
    event MenuEvent OnPlayClicked;
  }

  public struct gameParameter
  {
    public String name;
    public bool isH;
    public bool isActive;
  }  

  public class NewGameMenu : BaseMenu, INewGameMenu
  {
    private Button p1button, p2button, p3button, p4button, BackBtn, StartBtn, StartBtn2;
    private Button p1HTypebutton, p2HTypebutton, p3HTypebutton, p4HTypebutton;
    //private Button p2AiTypebutton, p3AiTypebutton, p4AiTypebutton;
    private Button p3Activebutton, p4Activebutton, btnBordoPulsanti;
    private Texture2D coinP, coinAI, coinX, coinOK;
    private static readonly int H_BLOCK_WIDTH = 380;
    private static readonly int H_BLOCK_HEIGHT = 100;
    private Rectangle gameBounds;
    private bool p2changeSide = false, p3changeSide = false, p4changeSide = false, p3ActivebuttonFlip = false, p4ActivebuttonFlip = false;
    private int direction = -1;

    //public gameParameter[] gameParameters = new gameParameter[4];

    private bool FourPlayersActive = false;

    private Texture2D box, boxPlayer, x, v, bordoPulsanti;


    SpriteBatch spriteBatch;

    private Color p1Color = Color.DeepSkyBlue, p2Color = Color.LawnGreen, p3Color = Color.Tomato, p4Color = Color.Yellow;
    private Color noActiveColor = new Color(128, 128, 128, 100);

    public NewGameMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      spriteBatch = Game.GetContext().SpriteBatch;
      background = game.Content.Load<Texture2D>(@"Images//Backgrounds//Bg_Legno");
      bordoPulsanti = game.Content.Load<Texture2D>(@"Images//Menu//bordoPulsanti");

      bounds = new Rectangle(0, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
      game.GetContext().gameParameters = new gameParameter[4];

      game.Services.AddService(typeof(INewGameMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(INewGameMenu));
      base.Dispose(disposing);
    }

    Random rnd = new Random();

    public override void Initialize()
    {
      coinP = Game.Content.Load<Texture2D>(@"Images//Menu//CoinP");
      coinAI = Game.Content.Load<Texture2D>(@"Images//Menu//CoinAI");
      coinX = Game.Content.Load<Texture2D>(@"Images//Menu//x");
      coinOK = Game.Content.Load<Texture2D>(@"Images//Menu//OK");
      boxPlayer = Game.Content.Load<Texture2D>(@"Images//Game//roundBox");

      gameBounds = Game.GraphicsDevice.Viewport.Bounds;
      //Game.GetContext().gameParameters[0].name = Locale.Strings.Player + "1";
      //Game.GetContext().gameParameters[1].name = Locale.Strings.Player + "2";
      //Game.GetContext().gameParameters[2].name = Locale.Strings.Player + "3";
      //Game.GetContext().gameParameters[3].name = Locale.Strings.Player + "4";     

      List<int> n= new List<int>();

     // Nomi player casuali
      for (int i = 0; i < 4; i++)
      {
        int r = rnd.Next(0, 10);
        if (!n.Contains(r))
          n.Add(r);
        else i--;

        r = n.Last();

        String name = Locale.Strings.Player + "i";

        if (r == 0) name = Locale.Strings.AiPlayerName00;
        if (r == 1) name = Locale.Strings.AiPlayerName01;
        if (r == 2) name = Locale.Strings.AiPlayerName02;
        if (r == 3) name = Locale.Strings.AiPlayerName03;
        if (r == 4) name = Locale.Strings.AiPlayerName04;
        if (r == 5) name = Locale.Strings.AiPlayerName05;
        if (r == 6) name = Locale.Strings.AiPlayerName06; 
        if (r == 7) name = Locale.Strings.AiPlayerName07;
        if (r == 8) name = Locale.Strings.AiPlayerName08;
        if (r == 9) name = Locale.Strings.AiPlayerName09;
        if (r == 0) name = Locale.Strings.AiPlayerName00;

        Game.GetContext().gameParameters[i].name = name;
      }

      Game.GetContext().gameParameters[0].name = Game.GetContext().DefaultName;

      Game.GetContext().gameParameters[0].isH = true;
      Game.GetContext().gameParameters[1].isH = false;
      Game.GetContext().gameParameters[2].isH = false;
      Game.GetContext().gameParameters[3].isH = false;

      Game.GetContext().gameParameters[0].isActive = true;
      Game.GetContext().gameParameters[1].isActive = true;
      Game.GetContext().gameParameters[2].isActive = false;
      Game.GetContext().gameParameters[3].isActive = false;
     
     

      // Player1
      // Name
      p1button = new Button(Game, @"Images//Game//roundBox", new Rectangle(((gameBounds.Center.X - boxPlayer.Bounds.Center.X / 2) / 2) + 20, (gameBounds.Bottom - boxPlayer.Bounds.Center.Y / 2) - 20, H_BLOCK_WIDTH, H_BLOCK_HEIGHT), Color.Lerp(Color.CornflowerBlue, Color.Transparent, 0.1f), "small_font", Game.GetContext().gameParameters[0].name, Color.White);
      AddButton(p1button, () =>
      {

        p1button.touched = true;
        StateManagement.StateManager.keyboardMenu.Visible = true;
        StateManagement.StateManager.keyboardMenu.Enabled = true;
        this.Enabled = false;

        var keyboard = Game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;
        keyboard.OnSaveClicked += () => 
        {
          Game.GetContext().gameParameters[0].name = StateManagement.StateManager.keyboardMenu.word;
          Game.GetContext().DefaultName = Game.GetContext().gameParameters[0].name;
          p1button.message = Game.GetContext().gameParameters[0].name;
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

        keyboard.OnExitClicked += () => 
        {
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

      });

      p1HTypebutton = new Button(Game, @"Images//Menu//CoinP", new Rectangle(p1button.bounds.Center.X - coinP.Width, p1button.bounds.Y - coinP.Height/2 - 20, 80, 80), Color.White, "small_font", "", Color.Green);
      AddButton(p1HTypebutton, () =>
      {});


      // Player 2
      p2button = new Button(Game, @"Images//Game//roundBox", new Rectangle(((gameBounds.Center.X - boxPlayer.Bounds.Center.X / 2) / 2) + 20, (gameBounds.Top - boxPlayer.Bounds.Center.Y / 2) / 2, H_BLOCK_WIDTH, H_BLOCK_HEIGHT), Color.Lerp(Color.CornflowerBlue, Color.Transparent, 0.1f), "small_font",Game.GetContext().gameParameters[1].name, Color.White);
      AddButton(p2button, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        p2button.touched = true;
        StateManagement.StateManager.keyboardMenu.Visible = true;
        StateManagement.StateManager.keyboardMenu.Enabled = true;
        this.Enabled = false;

        var keyboard = Game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;
        keyboard.OnSaveClicked += () => 
        {
          Game.GetContext().gameParameters[1].name = StateManagement.StateManager.keyboardMenu.word;
          p2button.message = Game.GetContext().gameParameters[1].name;
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();

        };

        keyboard.OnExitClicked += () =>
        {
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

      });



      p2HTypebutton = new Button(Game, @"Images//Menu//CoinP", new Rectangle(p2button.bounds.Center.X + coinP.Width - 10, p2button.bounds.Y + p2button.bounds.Height + coinP.Height/2 - 15, 80, 80), Color.White, "small_font","" , Color.White);
      p2HTypebutton.centeredOrigin = true;
      AddButton(p2HTypebutton, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

          if(FourPlayersActive)
            Game.GetContext().gameParameters[2].isH = !Game.GetContext().gameParameters[2].isH;
          else
             Game.GetContext().gameParameters[1].isH = !Game.GetContext().gameParameters[1].isH;
          
        p2changeSide = true;
     
      });

            
      //Player3
      
      p3button = new Button(Game, @"Images//Game//roundBox2", new Rectangle(gameBounds.Right - 220, gameBounds.Center.Y - 180, H_BLOCK_HEIGHT + 20, H_BLOCK_WIDTH - 25), Color.Lerp(Color.Gray, Color.Transparent, 0.2f), "small_font", Game.GetContext().gameParameters[2].name, Color.Gray);
      AddButton(p3button, () =>
      {

        if (!FourPlayersActive) return;        
                
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);        
         
        p3button.touched = true;
        StateManagement.StateManager.keyboardMenu.Visible = true;
        StateManagement.StateManager.keyboardMenu.Enabled = true;
        this.Enabled = false;

        var keyboard = Game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;
        keyboard.OnSaveClicked += () =>
        {
          Game.GetContext().gameParameters[2].name = StateManagement.StateManager.keyboardMenu.word;
          p3button.message = Game.GetContext().gameParameters[2].name;
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

        keyboard.OnExitClicked += () =>
        {
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

      });

      p3HTypebutton = new Button(Game, @"Images//Menu//CoinAI", new Rectangle(p3button.bounds.X - coinP.Width/2 + 15, gameBounds.Center.Y - coinP.Height + 50 , 80, 80), Color.White, "small_font", "", Color.White);
      p3HTypebutton.centeredOrigin = true;
      AddButton(p3HTypebutton, () =>
      {
        if (!FourPlayersActive) return;

        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        if (!FourPlayersActive) return;
        Game.GetContext().gameParameters[1].isH = !Game.GetContext().gameParameters[1].isH;
        p3changeSide = true;
      });


      p3Activebutton = new Button(Game, @"Images//Menu//OK", new Rectangle(p3button.bounds.X + coinOK.Width/2 - 90, gameBounds.Center.Y + coinOK.Height/2 + 25, 80, 80), noActiveColor, "small_font", "", Color.White);
      p3Activebutton.centeredOrigin = true;
      AddButton(p3Activebutton, () =>
      {
        if (activateTimer) return;

        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        p3ActivebuttonFlip = true;

        if (!FourPlayersActive)
        {
          Game.GetContext().gameParameters[2].isActive = true;
          Game.GetContext().gameParameters[3].isActive = true;
          FourPlayersActive = true;
          p3button.color = Color.Lerp(Color.CornflowerBlue, Color.Transparent, 0.1f);
          p4button.color = Color.Lerp(Color.CornflowerBlue, Color.Transparent, 0.1f);

          // cambio posto al giocatore 2
          var p2name = p2button.message;
          p2button.message = p3button.message;
          p3button.message = p2name;

          var p2Ai = Game.GetContext().gameParameters[1].isH;
          if (p2Ai)
            Game.GetContext().gameParameters[2].isH = true;
          else
            Game.GetContext().gameParameters[2].isH = false;

          return;
        }

        if (FourPlayersActive)
        {
          // cambio posto al giocatore 2
          var p2name = p2button.message;
          p2button.message = p3button.message;
          p3button.message = p2name;

          var p2Ai = Game.GetContext().gameParameters[2].isH;
          if (p2Ai)
            Game.GetContext().gameParameters[1].isH = true;
          else
            Game.GetContext().gameParameters[1].isH = false;

          // = !Game.GetContext().gameParameters[1].isH;

          Game.GetContext().gameParameters[2].isActive = false;
          Game.GetContext().gameParameters[3].isActive = false;
          FourPlayersActive = false;
          
        }
        
      });


      // Bottoni

      btnBordoPulsanti = new Button(Game, @"Images//Menu//BordoPulsanti", new Rectangle(gameBounds.Right - (bordoPulsanti.Width / 2 + 50), 0, 205, 490), Color.White, "small_font", "", Color.White);
      AddButton(btnBordoPulsanti, () =>
      {
      });

      // Back button
      BackBtn = new Button(Game, @"Images//Menu//button2", new Rectangle(gameBounds.Right - (bordoPulsanti.Width / 2 + 25), 350, 140, 55), Color.Red, "small_font", Locale.Strings.Back, Color.White);
      AddButton(BackBtn, () =>
      {
        BackBtn.touched = true;
        
      });

      //// Start button
      //StartBtn = new Button(Game, @"Images//Menu//button2", new Rectangle(gameBounds.Right - (bordoPulsanti.Width / 2 + 15), 150, 140, 55), Color.Green, "small_font", Locale.Strings.Tournament, Color.White);
      //AddButton(StartBtn, () =>
      //{
      //  StartBtn.touched = true;

      //  Game.GetContext().QuickGame = false;
      //  Game.GetContext().GameFinished = false;

      //  Game.GetContext().PlayerTurn = 0;

      //  //if (OnPlayClicked != null)
      //  //  OnPlayClicked();

      //});

      // Quick Game
      StartBtn2 = new Button(Game, @"Images//Menu//button2", new Rectangle(gameBounds.Right - (bordoPulsanti.Width / 2 + 25), 100, 140, 55), new Color(34, 200, 34), "small_font", Locale.Strings.Play, Color.White);
      AddButton(StartBtn2, () =>
      {
        StartBtn2.touched = true;

        Game.GetContext().QuickGame = true;
        Game.GetContext().GameFinished = false;
      });

      // Player 4

      p4button = new Button(Game, @"Images//Game//roundBox2", new Rectangle(gameBounds.Left - 40, gameBounds.Center.Y - 180, H_BLOCK_HEIGHT + 20, H_BLOCK_WIDTH - 25), Color.Lerp(Color.Gray, Color.Transparent, 0.2f), "small_font", Game.GetContext().gameParameters[3].name, Color.Gray);
      AddButton(p4button, () =>
      {

        if (!FourPlayersActive) return;

        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);                
        
        p4button.touched = true;
        StateManagement.StateManager.keyboardMenu.Visible = true;
        StateManagement.StateManager.keyboardMenu.Enabled = true;
        this.Enabled = false;

        var keyboard = Game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;

        keyboard.OnSaveClicked += () =>
        {
          Game.GetContext().gameParameters[3].name = StateManagement.StateManager.keyboardMenu.word;
          p4button.message = Game.GetContext().gameParameters[3].name;
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

        keyboard.OnExitClicked += () =>
        {
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };
      });

      p4HTypebutton = new Button(Game, @"Images//Menu//CoinAI", new Rectangle(p4button.bounds.Center.X + coinP.Width - 15, gameBounds.Center.Y + coinP.Height - 50, 80, 80), noActiveColor, "small_font", "", Color.White);
      p4HTypebutton.centeredOrigin = true;
      AddButton(p4HTypebutton, () =>
      {
        if (!FourPlayersActive) return;

        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        Game.GetContext().gameParameters[3].isH = !Game.GetContext().gameParameters[3].isH;
        p4changeSide = true;
      });

      p4Activebutton = new Button(Game, @"Images//Menu//OK", new Rectangle(p4button.bounds.Center.X + coinP.Width - 15, gameBounds.Center.Y - coinP.Height + 50, 80, 80), noActiveColor, "small_font", "", Color.White);
      p4Activebutton.centeredOrigin = true;
      AddButton(p4Activebutton, () =>
      {
        if (activateTimer) return;

        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        p3ActivebuttonFlip = true;

        if (!FourPlayersActive)
        {
          Game.GetContext().gameParameters[2].isActive = true;
          Game.GetContext().gameParameters[3].isActive = true;
          FourPlayersActive = true;
          p3button.color = Color.Lerp(Color.CornflowerBlue, Color.Transparent, 0.1f);
          p4button.color = Color.Lerp(Color.CornflowerBlue, Color.Transparent, 0.1f);

          // cambio posto al giocatore 2
          var p2name = p2button.message;
          p2button.message = p3button.message;
          p3button.message = p2name;

          var p2Ai = Game.GetContext().gameParameters[1].isH;
          if (p2Ai)
            Game.GetContext().gameParameters[2].isH = true;
          else
            Game.GetContext().gameParameters[2].isH = false;

          return;
        }

        if (FourPlayersActive)
        {
          // cambio posto al giocatore 2
          var p2name = p2button.message;
          p2button.message = p3button.message;
          p3button.message = p2name;

          var p2Ai = Game.GetContext().gameParameters[1].isH;
          if (p2Ai)
            Game.GetContext().gameParameters[2].isH = true;
          else
            Game.GetContext().gameParameters[2].isH = false;

          // = !Game.GetContext().gameParameters[1].isH;

          Game.GetContext().gameParameters[2].isActive = false;
          Game.GetContext().gameParameters[3].isActive = false;
          FourPlayersActive = false;
        }

      });      

      base.Initialize();
    }

    private void ResetKeyboard()
    {
      Game.Services.RemoveService(typeof(IKeyboardMenu));

      StateManagement.StateManager.keyboardMenu = new KeyboardMenu(Game, MenuDepths.ForeGround);
      Game.Components.Add(StateManagement.StateManager.keyboardMenu);

      StateManagement.StateManager.keyboardMenu.Animated = true;
      StateManagement.StateManager.keyboardMenu.Enabled = false;
      StateManagement.StateManager.keyboardMenu.Visible = false;
    }

    int counter = 4;
    float time = 0;
    bool activateTimer = true;
    public override void Update(GameTime gameTime)
    {

      if (activateTimer)
      {
        time += gameTime.dt();
        p3Activebutton.color = new Color(100,100,100, 0.1f + time);
        p4Activebutton.color = new Color(100, 100, 100, 0.1f + time);

      }

      if (time > 1.2f)
      {
        p3Activebutton.color = Color.White;
        p4Activebutton.color = Color.White;
        p2changeSide = true;
        activateTimer = false;
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);
        time = 0;
      }

      if (p2changeSide)
      {       
        p2HTypebutton.bounds.Width += direction * 30;

        if ((p2HTypebutton.bounds.Width <= 0) && (direction == -1))
        {          
          if(p2HTypebutton.texture.Equals(coinP))
            p2HTypebutton.texture = coinAI;
          else
            p2HTypebutton.texture = coinP;
          direction = 1;                  
        }
      
        if ((p2HTypebutton.bounds.Width > coinP.Width) && (direction == 1))
        {
          p2HTypebutton.bounds.Width = 80;
          direction = -1;          
          p2changeSide = false;
        }
      }


      if (p3changeSide)
      {
        p3HTypebutton.bounds.Width += direction * 30;

        if ((p3HTypebutton.bounds.Width <= 0) && (direction == -1))
        {
          if (p3HTypebutton.texture.Equals(coinP))
            p3HTypebutton.texture = coinAI;
          else
            p3HTypebutton.texture = coinP;
          direction = 1;
        }

        if ((p3HTypebutton.bounds.Width > coinP.Width) && (direction == 1))
        {
          p3HTypebutton.bounds.Width = 80;
          direction = -1;
          p3changeSide = false;
        }
      }

      if (p4changeSide)
      {
        p4HTypebutton.bounds.Width += direction * 30;

        if ((p4HTypebutton.bounds.Width <= 0) && (direction == -1))
        {
          if (p4HTypebutton.texture.Equals(coinP))
            p4HTypebutton.texture = coinAI;
          else
            p4HTypebutton.texture = coinP;
          direction = 1;
        }

        if ((p4HTypebutton.bounds.Width > coinP.Width) && (direction == 1))
        {
          p4HTypebutton.bounds.Width = 80;
          direction = -1;
          p4changeSide = false;          
        }
      }

      if (p3ActivebuttonFlip)
      {
        p3Activebutton.bounds.Width += direction * 30;
        p4Activebutton.bounds.Width += direction * 30;

        if ((p3Activebutton.bounds.Width <= 0) && (direction == -1))
        {
          if (p3Activebutton.texture.Equals(coinOK))
          {
            p3Activebutton.texture = coinX;
            p4Activebutton.texture = coinX;
          }
          else
          {
            p3Activebutton.texture = coinOK;
            p4Activebutton.texture = coinOK;
          }
          direction = 1;
        }

        if ((p3Activebutton.bounds.Width > coinX.Width) && (direction == 1))
        {
          p3Activebutton.bounds.Width = 80;
          p4Activebutton.bounds.Width = 80;
          direction = -1;
          p3ActivebuttonFlip = false;          
        }
      }

      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        if (OnBackClicked != null)
          OnBackClicked();  
        
      }

      if (FourPlayersActive)
      {
        // Cambio i colori dividendo per squadre
        p1button.color = Color.DodgerBlue;
        p3button.color = Color.OrangeRed;
        p2button.color = Color.DodgerBlue;
        p4button.color = Color.OrangeRed;

        p3HTypebutton.color = Color.White;
        p4HTypebutton.color = Color.White;
        //p3Activebutton.color = Color.White;
        //p4Activebutton.color = Color.White;
        //p3Activebutton.color = Color.White;
        p3button.textColor = Color.White;
        p4button.textColor = Color.White;

        if (p3button.bounds.X + p3button.bounds.Width > gameBounds.Right - (bordoPulsanti.Width / 2 + 45))
        {
          p3HTypebutton.color = Color.White;
          //p3Activebutton.color = Color.White;

          p3button.bounds.X -= 5;
          p3HTypebutton.bounds.X -= 5;
          p3Activebutton.bounds.X -= 5;
        }
      
        if (p4button.bounds.X < gameBounds.Left)
        {
          p4button.bounds.X += 5;
          p4HTypebutton.bounds.X += 5;
          p4Activebutton.bounds.X += 5;
        }
      }

      if (!FourPlayersActive)
      {
        p1button.color = Color.CornflowerBlue;
        p2button.color = Color.CornflowerBlue;

        p3button.color = Color.Lerp(Color.Gray, Color.Transparent, 0.2f);
        p4button.color = Color.Lerp(Color.Gray, Color.Transparent, 0.2f);
        p3HTypebutton.color = noActiveColor;
        p4HTypebutton.color = noActiveColor;
        //p3Activebutton.color = noActiveColor;
        //p4Activebutton.color = noActiveColor;
        p3button.textColor = Color.Gray;
        p4button.textColor = Color.Gray;

        if (p3button.bounds.X + p3button.bounds.Width < gameBounds.Right - (bordoPulsanti.Width / 2 + 45) + 50)
        {
          //p3AiTypebutton.color = noActiveColor;
          //p3Activebutton.color = noActiveColor;

          p3button.bounds.X += 5;
          p3HTypebutton.bounds.X += 5;
          //p3AiTypebutton.bounds.X += 5;
          p3Activebutton.bounds.X += 5;
        }

        if (p4button.bounds.X > gameBounds.Left - 50)
        {
          p4button.bounds.X -= 5;
          p4HTypebutton.bounds.X -= 5;
          //p4AiTypebutton.bounds.X -= 5;
          p4Activebutton.bounds.X -= 5;
        }


        
      }

      //if (StartBtn.touched)
      //{
      //  counter--;
      //  if (counter < 0)
      //  {
      //    StartBtn.touched = false;
      //    if ((OnPlayClicked != null))
      //      OnPlayClicked();
      //    counter = 4;
      //  }
      //}

      if (StartBtn2.touched)
      {
        counter--;
        if (counter < 0)
        {
          StartBtn2.touched = false;
          if ((OnPlayClicked != null))
            OnPlayClicked();
          counter = 4;
          return;
        }
      }

      if (BackBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          BackBtn.touched = false;
          if (OnBackClicked != null)
            OnBackClicked();
          counter = 4;
        }
      }

      if (p1button.touched)
      {
        counter--;
        if (counter < 0)
        {
          p1button.touched = false;
          counter = 4;
        }
      }

      if (p2button.touched)
      {
        counter--;
        if (counter < 0)
        {
          p2button.touched = false;
          counter = 4;
        }
      }

      if (p3button.touched)
      {
        counter--;
        if (counter < 0)
        {
          p3button.touched = false;
          counter = 4;
        }
      }

      if (p4button.touched)
      {
        counter--;
        if (counter < 0)
        {
          p4button.touched = false;
          counter = 4;
        }
      }

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();

     
      //spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Menu//CoinP"), new Rectangle(gameBounds.Top + 100, gameBounds.Top + 25, 100, 100), null, Color.White, 0.5f, Vector2.Zero, SpriteEffects.None, 1);
      //new Button(Game, @"Images//Menu//CoinP", new Rectangle(gameBounds.Top + 100, gameBounds.Top + 25, 100, 100), Color.Transparent, "small_font", "", Color.White);
      //spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Menu//CoinAI"), new Rectangle(gameBounds.Top + 200, gameBounds.Top + 25, 100, 100), null, Color.White, 0.5f, Vector2.Zero, SpriteEffects.None, 1);
    

      //if (Game.GetContext().gameParameters[1].isH) spriteBatch.Draw(box, new Rectangle(400, 160, 55, 55), Color.White);
      //else spriteBatch.Draw(box, new Rectangle(500, 160, 55, 55), Color.White);

      //if (Game.GetContext().gameParameters[2].isH) spriteBatch.Draw(box, new Rectangle(400, 260, 55, 55), Color.White);
      //else spriteBatch.Draw(box, new Rectangle(500, 260, 55,55), Color.White);

      //if (Game.GetContext().gameParameters[3].isH) spriteBatch.Draw(box, new Rectangle(400, 360, 55, 55), Color.White);
      //else spriteBatch.Draw(box, new Rectangle(500, 360, 55, 55), Color.White);
     
      spriteBatch.End();

    }

    public event MenuEvent OnBackClicked;
    public event MenuEvent OnPlayClicked;
  }
}
